package com.plant.consult.service.impl;

import cn.hutool.core.date.DateTime;
import com.plant.common.domain.User;
import com.plant.common.result.Result;
import com.plant.common.utils.IdWorker;
import com.plant.consult.domain.*;
import com.plant.consult.mapper.ConsultDetailMapper;
import com.plant.consult.mapper.ConsultMapper;
import com.plant.consult.mapper.ConsultRecordMapper;
import com.plant.consult.service.ConsultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ConsultServiceImpl implements ConsultService {
    @Autowired
    private ConsultMapper consultMapper;
    @Autowired
    private ConsultDetailMapper consultDetailMapper;
    @Autowired
    private ConsultRecordMapper consultRecordMapper;

    @Override
    public Consult add(Consult consult) {
        IdWorker idWorker = new IdWorker();
        consult.setConId(idWorker.nextId());
        consult.setCreateTime(new DateTime());
        consult.setStatus(0);
        consult.setDel(0);
        consultMapper.add(consult);
        return consult;
    }

    @Override
    public List<Consult> list() {
        return consultMapper.list();
    }

    @Override
    public List<Consult> userIdList(Integer userId) {
        return consultMapper.findByUserId(userId);
    }

    @Override
    public void money(Consult consult) {
        consultMapper.money(consult);
    }

    @Override
    public List<Consult> oneList(User user) {
        return consultMapper.oneList(user);
    }

    @Override
    public ConsultVo ReplyList(ConsultDetail consultDetail) {
//        查询会诊信息
        Consult consult = consultMapper.findById(consultDetail.getId());
        //根据问诊信息查询接诊信息
        ConsultRecord consultRecord = consultRecordMapper.findConId(consult.getConId());
        //根据问诊信息查询回复信息
        List<ConsultDetail> consultDetails = consultDetailMapper.findConId(consultRecord.getId());
        List<ConsultDetail> list = consultDetails.stream().filter(item -> item.getSupers() == 0).toList();
        for (ConsultDetail item : list) {
            List<ConsultDetail> replyList = consultDetails.stream().filter(item1 -> item1.getSupers() == item.getId()).toList();
            item.setConsultDetail(replyList);
        }
        ConsultVo consultVo = new ConsultVo();
        consultVo.setConsult(consult);
        consultVo.setConsultRecord(consultRecord);
        consultVo.setConsultDetail(list);
        return consultVo;
    }

    @Override
    @Transactional
    public void update(Consult consult) {
//        修改consult表status
        consultMapper.over(consult);
//        修改consult_record表status
        ConsultRecord consultRecord = new ConsultRecord();
        consultRecord.setConId(consult.getConId());
        consultRecord.setEndTime(new Date());
        consultRecordMapper.over(consultRecord);
//        查询出医师id
         Integer id= consultMapper.phId(consult);
//         根据id给用户添加金额
        consultMapper.userMoneyAdd(id,consult.getMoney());
    }
}
